//
//  XJMyOrderSectionFooterView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/26.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  订单区尾

#import "XJMyOrderSectionFooterView.h"
#import "UIButton+ZYButton.h"

#import "XJMyOrderBtnModel.h" //按钮model

@interface XJMyOrderSectionFooterView()
@property(nonatomic, strong) UIView *bgView; // 习惯

@property(nonatomic, strong) UIView *smallBgView;
@property(nonatomic, strong) UILabel *countLabel; // 共计1件商品合计
@property(nonatomic, strong) UILabel *totalPriceLabel; // ￥459.00
@property(nonatomic, strong) UILabel *remarksLabel; //备注   (含运费6元)

@property(nonatomic, strong) UIView *lightGrayLine; // 线


@property(nonatomic, copy) NSArray <XJMyOrderBtnModel *> *btnModelArr; //6-15


/**
  有两个按钮 文字不一样.
 外部需要提供一个接口控制: 1/有几个按钮显示 2/按钮的文字内容
 全部:   只有一个删除订单
 待付款:  取消订单 立即付款
 待收货:  申请售后 确认收货
 待评价:  申请售后 去评价
 已完成 : 删除订单 申请售后

 */
@property(nonatomic, strong) UIButton *firstButton;
@property(nonatomic, strong) UIButton *secondButton;


@end

@implementation XJMyOrderSectionFooterView

#pragma mark - 一 初始化
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
    }
    return self;
}
#pragma mark - 二 lazy

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UIView *)smallBgView {
    if (!_smallBgView) {
        _smallBgView = [[UIView alloc] init];
    }
    return _smallBgView;
}

- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.textAlignment = NSTextAlignmentRight;
    }
    return _countLabel;
}

// 字体加粗
- (UILabel *)totalPriceLabel {
    if (!_totalPriceLabel) {
        _totalPriceLabel = [[UILabel alloc] init];
        _totalPriceLabel.font = XJFONTSET_M(17);
        _totalPriceLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _totalPriceLabel;
}

- (UILabel *)remarksLabel{
    if (!_remarksLabel) {
        _remarksLabel = [[UILabel alloc] init];
        _remarksLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _remarksLabel;
}



// 记得设置frame
- (UIButton *)firstButton {
    if (!_firstButton) {
        _firstButton = [UIButton buttonWithFrame:CGRectZero target:self action:@selector(firstButtonAction) bgc:nil title:@"取消订单"];
    }
    return _firstButton;
}

- (UIButton *)secondButton {
    if (!_secondButton) {
        _secondButton = [UIButton buttonWithFrame:CGRectZero target:self action:@selector(secondButtonAction) bgc:nil title:@"立即付款"];
    }
    return _secondButton;
}

//h:1
- (UIView *)lightGrayLine {
    if (!_lightGrayLine) {
        _lightGrayLine = [UIView new];
        _lightGrayLine.backgroundColor = XJCOLOR_RGB(240,240,240);
    }
    return _lightGrayLine;
}



- (void)setupView {
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.smallBgView];
    [self.bgView addSubview:self.firstButton];
    [self.bgView addSubview:self.secondButton];
    
    
//    self.countLabel.backgroundColor = [UIColor redColor];
//    self.totalPriceLabel.backgroundColor = [UIColor greenColor];
//    self.remarksLabel.backgroundColor = [UIColor brownColor];
    [self.smallBgView addSubview:self.countLabel];
    [self.smallBgView addSubview:self.totalPriceLabel];
    [self.smallBgView addSubview:self.remarksLabel];
    
    [self.bgView addSubview:self.lightGrayLine];
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    
    // (10, 10, self.等宽, 高:30)
    [self.smallBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.top.equalTo(self.bgView.mas_top).offset(5);
        make.right.equalTo(self.bgView.mas_right).offset(10);
        make.height.mas_equalTo(30);
    }];
    
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.smallBgView);
        make.width.mas_offset(140);
    }];
    [self.totalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.countLabel.mas_right);
        make.width.mas_offset(80);
        make.centerY.equalTo(self.smallBgView.mas_centerY);
    }];
    [self.remarksLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalPriceLabel.mas_right);
        make.centerY.equalTo(self.smallBgView.mas_centerY);
        make.right.equalTo(self.smallBgView.mas_right);
    }];
    
    
    
    
    [self.firstButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 35));
        make.top.equalTo(self.smallBgView.mas_bottom).offset(10);
        make.right.equalTo(self.secondButton.mas_left).offset(-5);
    }];
    
    [self.secondButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 35));
        make.top.equalTo(self.smallBgView.mas_bottom).offset(10);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
    }];
    
    
    
    //线
    [self.lightGrayLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.height.mas_equalTo(1.0);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-2);
    }];
}
#pragma mark - 四 回调事件
- (void)firstButtonAction {
    if (self.myOrderSectionFooterViewFirstButtonBlock) {
        
        // 为什么敢这么写:服务器返回数组个数固定为1个或2个. 如果为两个两个按钮全显示。
        // 如果为一个的时候只显示一个按钮。
        if (self.btnModelArr.count ==2 ) {
            XJMyOrderBtnModel *firstBtnM = [self.btnModelArr firstObject];
            self.myOrderSectionFooterViewFirstButtonBlock(firstBtnM);
        }
    }
}

- (void)secondButtonAction {

    if (self.myOrderSectionFooterViewSecondButtonBlock) {
        
        if (self.btnModelArr.count) {
            XJMyOrderBtnModel *secondBtnM = [self.btnModelArr lastObject];
            self.myOrderSectionFooterViewSecondButtonBlock(secondBtnM);
        }
    }
}



// 布局欠缺的是 变化的状态怎么适配。 写成固定的。。。。。。

- (void)configureXJMyOrderSectionFooterViewHideFirstButton:(BOOL)hide count:(NSString *)count price:(NSString *)price remarks:(NSString *)remark fistButtonTitle:(NSString *)firstTitle secondButtonTitle:(NSString *)secondTitle {
    if (hide) {
        self.firstButton.hidden = YES;
    }else {
        self.firstButton.hidden = NO;
    }
    
    self.countLabel.text = [NSString stringWithFormat:@"共%@件商品，合计",count];
    

    self.totalPriceLabel.text = [NSString stringWithFormat:@"￥%.2f",[price floatValue]];
    self.remarksLabel.text = [NSString stringWithFormat:@"(%@)",remark];
    [self.firstButton setTitle:firstTitle forState:(UIControlStateNormal)];
    [self.secondButton setTitle:secondTitle forState:(UIControlStateNormal)];
}


#pragma mark - configure
- (void)configureXJMyOrderSectionFooterViewCount:(NSString *)count price:(NSString *)price freight:(NSString *)freight bntArray:(NSArray *)btnArr {
    self.countLabel.text = [NSString stringWithFormat:@"共%@件商品，合计",count];

    self.totalPriceLabel.text = [NSString stringWithFormat:@"￥%.2f",[price floatValue]];
    
    self.remarksLabel.text = [NSString stringWithFormat:@"(含运费%.1f)",[freight floatValue]];
    
    
    self.btnModelArr = btnArr;
    
    // @"立即付款"  @"取消订单"
    // 如果数组 2 个元素。 两个按钮都显示
    // 如果数组 1 个元素   只显示最后一个按钮。
    // 其他情况(0个 、>2 服务器应该不会返回2个以上) 不显示
    if (btnArr.count ==2) {
    
        self.firstButton.hidden = NO;
        self.secondButton.hidden = NO;
        
        
        XJMyOrderBtnModel *firtBtnM = [btnArr firstObject];
        [self.firstButton setTitle:firtBtnM.text forState:(UIControlStateNormal)];
        
        XJMyOrderBtnModel *secondBtnM = [btnArr lastObject];
        [self.secondButton setTitle:secondBtnM.text forState:(UIControlStateNormal)];
        
        
    
    }else if(btnArr.count == 1) {
        self.firstButton.hidden = YES;
        XJMyOrderBtnModel *btnModel = [btnArr firstObject];
        
        [self.secondButton setTitle:btnModel.text forState:(UIControlStateNormal)];
    }else {
        self.firstButton.hidden = YES;
        self.secondButton.hidden = YES;
    }
}

@end
